﻿using System;

namespace Spiral
{
    /// <summary>
    /// Outputs numbers in the console. The numbers are sorted and form a spiral. The spiral is actually a square matrix.
    /// </summary>
    public class Spiral
    {
        //The user message shown to prompt the user to enter the spiram matrix dimention.
        private static string UserInputMessage = "Please enter the dimention of the spiral matrix (integer): ";

        /// <summary>
        ///  The main entry method of the class. 
        /// </summary>
        /// <param name="args">The console parameters.</param>
        static void Main(string[] args)
        {
            Console.Write(UserInputMessage);

            //The dimention of the spiral matrix.
            int matrixDimention = 0;

            //The user is propmted to enter an integer value untill he really does so.
            while (!int.TryParse(Console.ReadLine(), out matrixDimention))
            {
                Console.Write(UserInputMessage);
            }

            //The spiral matrix.
            int[,] spiralMatrix = new int[matrixDimention, matrixDimention];

            MatrixNavigator navigator = new MatrixNavigator(matrixDimention);

            //Follow the steps of the navigator and everytime add an incremented value.
            for (int counter = 1; counter <= (matrixDimention * matrixDimention); counter++)
            {
                var step = navigator.GetStep();
                spiralMatrix[step.X - 1, step.Y - 1] = counter;
            }

            //Output the matrix spiral
            for (int i = 0; i < matrixDimention; i++)
            {
                for (int j = 0; j < matrixDimention; j++)
                {
                    Console.Write("{0,3}", spiralMatrix[j, i]);
                }

                Console.WriteLine();
            }

            Console.WriteLine();
            Console.Read();
        }
    }
}
